package com.chixing.controller;

import com.chixing.commons.ServerResponse;
import com.chixing.commons.UploadImgUtil;
import com.chixing.pojo.CustomerTokenDTO;
import com.chixing.pojo.Myorder;
import com.chixing.pojo.Users;
import com.chixing.service.IMycouponService;
import com.chixing.service.IMyorderService;
import com.chixing.service.IUsersService;
import com.chixing.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PersonalController {

    @Autowired
    private IUsersService usersService;

    @Autowired
    private IMyorderService myorderService;

    @Autowired
    private IMycouponService mycouponService;

    //加载个人用户信息
    @GetMapping("testPerson")
    public ServerResponse loadPersonData(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = TokenUtil.parseToken(token).getCustId();
        System.out.println("person token:"+token);

        ServerResponse user = null;
        List<Myorder> myorderlist = null;
        Long couponNum = 0L;

        if (token !=null){
            //获取用户数据
            user = usersService.getById(userId);
            //获取订单数据
            myorderlist = myorderService.getAllOrderById(userId);
            //获取优惠券数量
            couponNum = mycouponService.getCouponNumberByUserId(String.valueOf(userId));
            System.out.println("2222222222222"+userId);
        }else {
            return ServerResponse.fail("fail",null);
        }
        //将查询到的数据封装成map对象
        Map<String,Object> map = new HashMap<>();
        map.put("user",user);
        map.put("myorderlist",myorderlist);
        map.put("couponNum",couponNum);

        return ServerResponse.success("ok",map);
      /*  if (token !=null){
            user = TokenUtil.parseToken(toke);
        }*/
    }

    @PostMapping("postModifyMsg")
    public ServerResponse ModifyPersonalMsg(Users user, HttpServletRequest request, MultipartFile file){
        String token = request.getHeader("token");
        Integer userId = TokenUtil.parseToken(token).getCustId();
        //前端获取到的data数据被封装成user类
        String userName = user.getUserNickname();
        Long userTelno = user.getUserTelno();
        String userHeading = user.getUserHeading();
        System.out.println("22222222222:"+userHeading);
        //获取当前数据库数据
        Users user1 = usersService.getUpdateUsersById(userId);
        user1.setUserNickname(userName);//更新数据
        user1.setUserTelno(userTelno);
        try {
            String imgUrl = "http://"+ UploadImgUtil.upload(file.getBytes(),file.getOriginalFilename());
            if (imgUrl != null){
                user1.setUserHeading(imgUrl);
                UploadImgUtil.deleteFile(userHeading.substring(19));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //将更新后数据传入数据库
        ServerResponse result = usersService.updateUser(user1);
        return result;
    }

}
